var gulp = require("gulp"),
 	webserver = require("gulp-webserver"),
 	livereload = require("gulp-livereload"),
 	sass = require("gulp-ruby-sass"),
 	uglify = require("gulp-uglify"),
 	rename = require("gulp-rename"),
 	sourcemap = require("gulp-sourcemaps"),
 	tinypng = require("gulp-tinypng");
 	
 	
//1、自动刷新网页
gulp.task("webserver",function(){
 		gulp.src("dest")
 		.pipe(webserver({
 			livereload:true,
 			open:true
 		}))
 	})
//2、把html文件从开发环境 发布到发布环境
gulp.task("html",function(){
	return gulp.src("src/**/*.html")
	.pipe(gulp.dest("dest"))
})
//4、编译sass文件
gulp.task("sass",function(){
	return sass("src/sass/*.scss")
	.on("error",function(err){
		console.log(err)
	})
	.pipe(gulp.dest("dest/css"))
})
//5、js文件压缩&重命名
gulp.task("js",function(){
	return gulp.src("src/js/*.js")
	.pipe(sourcemap.init()) //js文件刚读入时，将js文件初始化到地图
	.pipe(uglify())
	.pipe(rename({suffix:".min"})) //重命名 压缩添加后缀
	.pipe(sourcemap.write("map")) 
	.pipe(gulp.dest("dest/js"))
})
//图片
gulp.task('tinypng', function(){      
	return gulp.src('src/images/**/*')       
	.pipe(tinypng('填写TinyPN API KEY'))      
	.pipe(gulp.dest('dest/images'));
});

//3、监听任务 
gulp.task("watch",function(){
	gulp.watch("src/**/*.html",["html"]);
	gulp.watch("src/sass/*.scss",["sass"]);
	gulp.watch('src/images/**/*.{png,jpg,gif,svg}', ['images']);
	gulp.watch("src/js/*.js",["js"]);
	 


})

gulp.task("default",["webserver","html","sass","js","watch"])
